﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using CorreoSocialWeb.Models;

namespace CorreoSocialWeb.Rutas
{
    public partial class EnviosDeRuta : System.Web.UI.Page
    {
        private int RutaDeEnvioId
        {
            get
            {
                return int.Parse(Request["RutaDeEnvioId"]);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (this.IsPostBack)
                return;

            Views.ReturnUrlHelper.Guardar(this);

            var titulo = string.Format("Detalle de Envíos de la ruta \"{0}\""
                , new CorreoSocialDbContext().Rutas.First(x => x.Id.Equals(this.RutaDeEnvioId)).OrigenDestinoString);

            this.Title = titulo;
            this.LabelTitulo.Text = titulo;
        }

        protected void ButtonVolver_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Rutas/MisRutas");
        }

        public IQueryable<CorreoSocialWeb.Models.RutaDeEnvio> GridView1_GetData()
        {
            return Ruta.GetParaDetalle(this.RutaDeEnvioId).Envios.ToList().AsQueryable();
        }

        protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rutaDeEnvioId;

            int.TryParse(e.CommandArgument.ToString(), out rutaDeEnvioId);

            switch (e.CommandName.Trim().ToLower())
            {
                case "aceptarrechazar":
                    Response.Redirect(string.Format("~/Rutas/ConfirmarSolicitudDeEnvio?RutaDeEnvioId={0}", rutaDeEnvioId));
                    break;

                case "cancelarentransito":
                    Response.Redirect(Views.CalificarUsuarioURLHelper.GetURL(RutaDeEnvio.GetParaConfirmar(rutaDeEnvioId), true, true));
                    break;

                case "marcarentregado":
                    Response.Redirect(Views.CalificarUsuarioURLHelper.GetURL(RutaDeEnvio.GetParaConfirmar(rutaDeEnvioId), false, true));
                    break;

                //case "cancelarenruta":
                //case "cancelardespacho":
                //    Response.Redirect(Views.CalificarUsuarioURLHelper.GetURL(Envio.GetPorId(envioId).RutaDeEnvioAceptada, true, false));
                //    break;
            }
        }
    }
}